10 ' mandelbr.bas - FreeWare 2005
20 GOTO 60 ' begin
30 SAVE "mandelbr.bas",A:LIST-40
40 GOTO 480 ' wait for key
50 GOTO 510 ' centered text
60 DEFSTR M,Q:Q=MKI$(0):SCREEN 9
70 CLS:PI=4*ATN(1):WINDOW (-320,-240)-(319,239)
80 DEFDBL A,B,C,D,S,X,Y:DIM COL(8),DOL(8)
90 DATA 0,1,9,2,10,4,12,6,14
100 DATA 0,1,9,2,10,4,12,14,6
110 FOR I=0 TO 8:READ COL(I):NEXT
120 FOR I=0 TO 8:READ DOL(I):NEXT
130 M(1)="MANDELBROT Generator idea from Hans Lauwerier"
140 M(2)="GWBasic by Eric Tchong"
150 M(3)="Press any key to halt and return to this screen"
160 M(4)="Choose 1..3   -1 = stop program "
170 FOR I=1 TO 4
180  GOSUB 50:IF I=2 OR I=3 THEN PRINT
190 NEXT
200 LOCATE 7,30:INPUT CH
210 IF CH=-1 THEN SCREEN 0,0,0:CLS:END
220 IF CH<1 OR CH>3 THEN 200
230 ' select
240 CLS:ON CH GOTO 260,540,770
250 ' mandelbrot 1
260 DELH=1.6:DELV=1.34:AC=-.65:N1=260:N2=INT(N1*DELV/DELH)
270 LINE (N1+1,N2+1)-(-N1-1,-N2-1),4,B
280 FOR I=-N1 TO N1:A=AC+I*DELH/N1
290  FOR J=0 TO N2:B=J*DELV/N2
300   U=4*(A*A+B*B):V=U-2*A+1/4
310   IF U+8*A+15/4<0 THEN L=0:GOTO 430
320   IF V-SQR(V)+2*A-1/2<0 THEN L=0:GOTO 430
330   X=A:Y=B:K=0
340   LSET Q=MKI$(0)
350    WHILE CVI(Q)=0
360     Z=X:X=X*X-Y*Y+A:Y=2*Z*Y+B
370     S=X*X+Y*Y:K=K+1
380     IF S>100 OR K=50 THEN 410
390    WEND
400   GOTO 340
410   IF K<40 THEN L=1+K MOD 8 ELSE L=0
420   IF K>3 THEN PSET (I,J),COL(L):PSET (I,-J),COL(L)
430   IF INKEY$<>"" THEN 460
440  NEXT
450 NEXT
460 GOSUB 40:CLS:GOTO 170
470 ' wait for key
480 LSET Q=MKI$(0)
490 WHILE CVI(Q)=0:MID$(Q,1)=INKEY$:WEND:RETURN
500 ' centered text
510 X=(80-LEN(M(I)))/2
520 PRINT TAB(X) M(I):RETURN
530 ' mandelbrot 2
540 DELH=PI:DELV=.75*PI:N1=240:N2=INT(N1*DELV/DELH)
550 FOR I=0 TO N1
560  FOR J=0 TO N2
570   IF INKEY$<>"" THEN 750
580   A=DELH*I/N1:B=DELV*J/N2
590   X=PI/2:Y=0
600    FOR K=1 TO 64
610     IF ABS(Y)>12 THEN L=1+K MOD 8:K=2005:GOTO 680
620     U=EXP(Y):V=1/U:CH=(U+V)/2:SH=(U-V)/2
630     SS=SIN(X):CS=COS(X)
640     X1=A+PI*SS*CH:Y1=B+PI*CS*SH
650     DIST=ABS(X-X1)+ABS(Y-Y1)
660     IF DIST<.001 THEN L=3:GOTO 710
670     X=X1:Y=Y1
680    NEXT
690    IF K=2006 THEN 710
700    L=3
710    PSET (I,J),DOL(L):PSET (I,-J),DOL(L)
720    PSET (-I,J),DOL(L):PSET (-I,-J),DOL(L)
730  NEXT J
740 NEXT
750 GOSUB 40:CLS:GOTO 170
760 ' mandelbrot 3
770 DELH=.8:DELV=.8:N1=200:N2=INT(N1*DELV/DELH)
780 FOR I=0 TO N1:A=I*DELH/N1
790  FOR J=0 TO N2:B=J*DELV/N2
800   X=1:Y=0
810   FOR K=1 TO 100
820    S=X*X+Y*Y:S2=S*S+1E-08
830    IF S>64 THEN L=1+K MOD 14:K=2005:GOTO 880
840    X2=X*X-Y*Y:Y2=2*X*Y
850    XR=X2/S2:YR=-Y2/S2
860    X1=X2+XR:Y1=Y2+YR
870    X=A*X1-B*Y1:Y=A*Y1+B*X1
880   NEXT
890   IF K=2006 THEN 910
900   L=9
910   PSET (I,J),L :PSET (I,-J),L
920   PSET (-I,J),L:PSET (-I,-J),L
930   IF INKEY$<>"" THEN 960
940  NEXT J
950 NEXT
960 GOSUB 40:CLS:GOTO 170
